package org.liaohailong.victor;

import org.junit.Test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest4 {

    @Test
    public void simplifyPath() {
        Daily daily = new Daily();

        String[] paths = {
                "/home/",
                "/../",
                "/home//foo/",
                "/a/./b/../../c/",
                "/",
                "/a//b////c/d//././/..",
        };

        for (String path : paths) {
            String s = daily.simplifyPath(path);
            log("s = " + s);
        }
    }

    @Test
    public void searchMatrix() {
        Daily daily = new Daily();


        int[][] matrix = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}};
        int target = 3;

//        int[][] matrix = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}};
//        int target = 13;

//        int[][] matrix = {{-10,-8}, {-6,-5}, {-2,-2}, {-1,0}, {3,4}, {7,7}, {8,9},
//                {10,10}, {11,11}, {12,14}, {15,16},
//                {17,19}, {20,21}, {22,22}, {25,27}, {28,30}, {32,32}, {35,36}};
//        int target = 16;

//        int[][] matrix = {{1}};
//        int target = 0;
        boolean search = daily.searchMatrix(matrix, target);
        log("search: %s", search);
    }

    @Test
    public void combine() {
        Daily daily = new Daily();

//        int n = 4;
//        int k = 2;
        int n = 1;
        int k = 1;
        List<List<Integer>> combine = daily.combine(n, k);
        log(combine.toString());
    }

    private void log(int[] arr) {
        if (arr.length > 100) {
            int[] shorter = new int[100];
            System.arraycopy(arr, 0, shorter, 0, 100);
            arr = shorter;
        }
        log(Arrays.toString(arr));
    }

    private void log(LinkedList<Integer> list) {
        int count = Math.min(100, list.size());

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            sb.append(next);
            count--;
            if (count <= 0) break;
            if (iterator.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append("]");
        log(sb.toString());
    }

    private void log(String msg, Object... args) {
        String format = String.format(msg, args);
        System.out.println(format);
    }

    private long calc(Runnable runnable) {
        long startMs = System.currentTimeMillis();
        runnable.run();
        return System.currentTimeMillis() - startMs;
    }
}